//
//  ZMAddressListVC.swift
//  zhonglibang
//
//  Created by Rowling on 2021/1/8.
//

import UIKit

class ZMAddressListVC: ZMViewController,ListRequestObjct {

    var listData = [AddressModel]()
    var page = 1
    var pageSize = 10
    var refreshScrollView : UIScrollView {
        get {
            return tableView
        }
    }
    
    
    // 新建收货地址按钮
    @IBOutlet weak var newAddressButton: UIButton!
    
    @IBOutlet weak var tableView: UITableView!
    
    /// 选择地址回调
    public var chooseAddressBlock : ((_ address : AddressModel)->())?
    
    public var chooseAddressBlockNopush : ((_ address : AddressModel)->())?

    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        self.tableView.mj_header?.beginRefreshing()
    }
    
    func setUI() {
        title = "收货地址"
        tableView.delegate = self
        tableView.dataSource = self
        tableView.zm.register(nibName: "ZMAddressCell", reuserId: "ZMAddressCell")
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 108
        tableView.backgroundColor = "#F7F7F9".zm.hex()
        tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
        addNodataScrollView(scrollView: tableView)
        emptyImg = "empty_address"
        emptyTitle = "您还没有添加地址哦"
        addRefresh(scrollView: tableView) { [unowned self] (header) in
            if header {self.page = 1}
            self.loadList()
        }
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        newAddressButton.style.DefaultGradientLayer()
    }
    
    //MARK:--------------------actions--------------------
    /// 新建收货地址
    @IBAction func newAddressClick(_ sender: Any) {
        if listData.count >= 20 {
            ZMToast.showToast(title: "地址最多添加20个")
            return
        }
        let vc = ZMEditeAddressVC()
        vc.successBlock = {(_) in
            self.tableView.mj_header?.beginRefreshing()
        }
        navigationController?.pushViewController(vc, animated: true)
    }
    
    
    //MARK:--------------------请求--------------------
    func loadList() {
        ZMUtil.listRequest(type: AddressModel.self, api: MineAPI.addressList(["page" : page,"limit" : pageSize])) { resp in
            self.listRequest(resp)
        }
    }
}


extension ZMAddressListVC : UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        tableView.mj_footer?.isHidden = listData.count == 0
        return listData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let data = listData[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "ZMAddressCell") as! ZMAddressCell
        cell.defaultView.isHidden = !data.isDefault
        cell.addressLabel.text = data.area + " " + data.detail
        cell.nickNameLabel.text = data.realName + " " + data.phone.zm.hidPhone()
        cell.editeButton.rx.tap.subscribe(onNext:{ [weak self] r in
            guard let self = self else {return}
            let vc = ZMEditeAddressVC()
            vc.successBlock = { [weak self] (_) in
                guard let self = self else {return}
                self.tableView.mj_header?.beginRefreshing()
            }
            vc.address = data
            vc.type = .editeAddress
            self.navigationController?.pushViewController(vc, animated: true)
        }).disposed(by: cell.disposeBag)
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let chooseAddressBlock = chooseAddressBlock {
            chooseAddressBlock(listData[indexPath.row])
            navigationController?.popViewController(animated: true)
        }
        
        if let chooseAddressBlockNopush = chooseAddressBlockNopush {
            chooseAddressBlockNopush(listData[indexPath.row])
        }
    }
}
